package cn.edu.henu.team.MPChart;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.table.health_p;
import cn.edu.henu.team.table.healthpUtils;
import cn.edu.henu.team.utils.CalendarDialog;
import cn.edu.henu.team.utils.ChartUtil;

public class LineAnalyseActivity extends BaseActivity {
    private int now_year;
    private int now_month;

    int selectPos = -1, selectMonth = -1,selectYear=-1;
    private List<health_p> data;
    private final List<LineEntry> mLineEntry1 = new ArrayList<>();
    private final List<LineEntry> mLineEntry2 = new ArrayList<>();
    private final List<LineEntry> mLineEntry3 = new ArrayList<>();
    private final String[] kinds = {"龋病", "牙髓炎", "根尖周炎"};
    private ImageView back;
    @BindView(R.id.chart_riLi)
    ImageView riLi;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.tv1)
    TextView btn1;
    @BindView(R.id.tv2)
    TextView btn2;
    @BindView(R.id.tv3)
    TextView btn3;

    private LineChart mLineChart;
    private Intent mIntent;

    @Override
    protected int initLayout() {
        return R.layout.activity_line_analyse;
    }

    @Override
    protected void initView() {
        mLineChart = findViewById(R.id.lineChart);
        ChartUtil.initChart(mLineChart);  //生成单条折线图

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Calendar calendar = Calendar.getInstance();
        now_year = calendar.get(Calendar.YEAR);
        now_month = calendar.get(Calendar.MONTH) + 1;
        selectYear = now_year;
        selectMonth = now_month;
        time.setText("当前日期: " + now_year + " 年 " + now_month + "月");
    }

    @Override
    protected void initData() {
        data = healthpUtils.queryListByYearAndMonth(now_year, now_month);

        for (health_p t : data) {
            //Log.d("***********","$$$$$$$$$"+t.getDay()+"****"+t.getP2());
            mLineEntry1.add(new LineEntry(t.getDay(), t.getP1())); //获取一条曲线的数据
            mLineEntry2.add(new LineEntry(t.getDay(), t.getP2())); //获取一条曲线的数据
            mLineEntry3.add(new LineEntry(t.getDay(), t.getP3())); //获取一条曲线的数据
        }

        ChartUtil.showLineChart(mLineChart, mLineEntry1, kinds[0], R.color.green);
        ChartUtil.addLine(mLineChart, mLineEntry2, kinds[1], R.color.textRed);
        ChartUtil.addLine(mLineChart, mLineEntry3, kinds[2], R.color.textLightBlue);
        Drawable drawable = getResources().getDrawable(R.drawable.fade_blue);
        ChartUtil.setChartFillDrawable(mLineChart, drawable);
        ChartUtil.setMarkerView(mLineChart, getApplicationContext());
    }

    @OnClick({R.id.chart_riLi,R.id.tv1,R.id.tv2,R.id.tv3})
    protected void OnClick(View v) {
        switch (v.getId()) {
            case R.id.chart_riLi:
                showCalendarDialog();
                break;
            case R.id.tv1:
                mIntent = new Intent(getApplicationContext(),LineDetalActivity.class);
                mIntent.putExtra("year",selectYear);
                mIntent.putExtra("month",selectMonth);
                mIntent.putExtra("kind",kinds[0]);
                startActivity(mIntent);
                break;
            case R.id.tv2:
                mIntent = new Intent(getApplicationContext(),LineDetalActivity.class);
                mIntent.putExtra("year",selectYear);
                mIntent.putExtra("month",selectMonth);
                mIntent.putExtra("kind",kinds[1]);
                startActivity(mIntent);
                break;
            case R.id.tv3:
                mIntent = new Intent(getApplicationContext(),LineDetalActivity.class);
                mIntent.putExtra("year",selectYear);
                mIntent.putExtra("month",selectMonth);
                mIntent.putExtra("kind",kinds[2]);
                startActivity(mIntent);
                break;
            default:
                break;
        }
    }

    /* 显示日历对话框*/
    private void showCalendarDialog() {
        CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
            @Override
            public void onRefresh(int selPos, int year, int month) {
                LineAnalyseActivity.this.selectPos = selPos;
                LineAnalyseActivity.this.selectYear = year;
                LineAnalyseActivity.this.selectMonth = month;
                time.setText("当前日期: " + year + " 年 " + month + "月");
                Log.d("******", "++++++ " + year);
                Log.d("******", "++++++ " + selectMonth);

                data.clear();
                mLineEntry1.clear();
                mLineEntry2.clear();
                mLineEntry3.clear();

                data = healthpUtils.queryListByYearAndMonth(year, selectMonth);

                for (health_p t : data) {
                    mLineEntry1.add(new LineEntry(t.getDay(), t.getP1()));
                    mLineEntry2.add(new LineEntry(t.getDay(), t.getP2()));
                    mLineEntry3.add(new LineEntry(t.getDay(), t.getP3()));
                }

                ChartUtil.resetLine(0, mLineChart, mLineEntry1, kinds[0], R.color.green);
                ChartUtil.resetLine(1, mLineChart, mLineEntry2, kinds[1], R.color.textRed);
                ChartUtil.resetLine(2, mLineChart, mLineEntry3, kinds[2], R.color.textLightBlue);
                Drawable drawable = getResources().getDrawable(R.drawable.fade_blue);
                ChartUtil.setChartFillDrawable(mLineChart, drawable);
                ChartUtil.setMarkerView(mLineChart, getApplicationContext());
            }
        });
    }
}